﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Windows;

namespace LaunchPad.Core
{
    public class ObservablePoint : INotifyPropertyChanged
    {
        public ObservablePoint()
        {
        }

        public ObservablePoint(double x, double y)
        {
            _x = x;
            _y = y;
        }

        private double _x;
        private double _y;

        [XmlAttribute]
        public double X
        {
            get { return _x; }
            set
            {
                if (_x != value)
                {
                    _x = value;
                    OnPropertyChanged("X");
                }
            }
        }

        [XmlAttribute]
        public double Y
        {
            get { return _y; }
            set
            {
                if (_y != value)
                {
                    _y = value;
                    OnPropertyChanged("Y");
                }
            }
        }

        public double Distance(ObservablePoint other)
        {
            return System.Math.Sqrt(System.Math.Pow(other.X - X, 2) + System.Math.Pow(other.Y - Y, 2));
        }

        public double[] ToArray()
        {
            var a = new double[2];
            a[0] = X;
            a[1] = Y;
            return a;
        }

        public Point ToPoint()
        {
            return new Point(X, Y);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
